<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="招聘行业" prop="industry">
              <el-select v-model="queryParams.industry" placeholder="请选择招聘行业" clearable >
                <el-option v-for="dict in infopedia_recruitment_industry_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="岗位名称" prop="position">
              <el-input v-model="queryParams.position" placeholder="请输入岗位名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="雇佣类型" prop="employmentType">
              <el-select v-model="queryParams.employmentType" placeholder="请选择雇佣类型" clearable >
                <el-option v-for="dict in infofepia_recruitment_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="公司类型" prop="companyType">
              <el-select v-model="queryParams.companyType" placeholder="请选择公司类型" clearable >
                <el-option v-for="dict in infopedia_company_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="薪资范围" prop="salaryRange">
              <el-input v-model="queryParams.salaryRange" placeholder="请输入薪资范围" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['infopedia:recruitment:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['infopedia:recruitment:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['infopedia:recruitment:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['infopedia:recruitment:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="recruitmentList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" v-if="true" />
        <el-table-column label="招聘标题" align="center" prop="title" />
        <el-table-column label="招聘行业" align="center" prop="industry">
          <template #default="scope">
            <dict-tag :options="infopedia_recruitment_industry_type" :value="scope.row.industry"/>
          </template>
        </el-table-column>
        <el-table-column label="岗位名称" align="center" prop="position" />
        <el-table-column label="雇佣类型" align="center" prop="employmentType">
          <template #default="scope">
            <dict-tag :options="infofepia_recruitment_type" :value="scope.row.employmentType"/>
          </template>
        </el-table-column>
        <el-table-column label="公司类型" align="center" prop="companyType">
          <template #default="scope">
            <dict-tag :options="infopedia_company_type" :value="scope.row.companyType"/>
          </template>
        </el-table-column>
        <el-table-column label="薪资范围" align="center" prop="salaryRange" />
        <el-table-column label="任职要求" align="center" prop="requirements" />
        <el-table-column label="福利" align="center" prop="welfare" />
        <el-table-column label="工作地址" align="center" prop="address" />
        <el-table-column label="首次发布时间" align="center" prop="publishTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="浏览量" align="center" prop="views" />
        <el-table-column label="分享数" align="center" prop="shares" />
        <el-table-column label="联系人" align="center" prop="contactPerson" />
        <el-table-column label="联系备注" align="center" prop="contactNote" />
        <el-table-column label="联系电话" align="center" prop="contactPhone" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['infopedia:recruitment:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['infopedia:recruitment:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改求职招聘信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="recruitmentFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="招聘标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入招聘标题" />
        </el-form-item>
        <el-form-item label="招聘行业" prop="industry">
          <el-select v-model="form.industry" placeholder="请选择招聘行业">
            <el-option
                v-for="dict in infopedia_recruitment_industry_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="岗位名称" prop="position">
          <el-input v-model="form.position" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="雇佣类型" prop="employmentType">
          <el-select v-model="form.employmentType" placeholder="请选择雇佣类型">
            <el-option
                v-for="dict in infofepia_recruitment_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司类型" prop="companyType">
          <el-select v-model="form.companyType" placeholder="请选择公司类型">
            <el-option
                v-for="dict in infopedia_company_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资范围" prop="salaryRange">
          <el-input v-model="form.salaryRange" placeholder="请输入薪资范围" />
        </el-form-item>
        <el-form-item label="任职要求" prop="requirements">
            <el-input v-model="form.requirements" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="福利" prop="welfare">
            <el-input v-model="form.welfare" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="工作地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入工作地址" />
        </el-form-item>
        <el-form-item label="首次发布时间" prop="publishTime">
          <el-date-picker clearable
            v-model="form.publishTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择首次发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="浏览量" prop="views">
          <el-input v-model="form.views" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="分享数" prop="shares">
          <el-input v-model="form.shares" placeholder="请输入分享数" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系备注" prop="contactNote">
          <el-input v-model="form.contactNote" placeholder="请输入联系备注" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Recruitment" lang="ts">
import { listRecruitment, getRecruitment, delRecruitment, addRecruitment, updateRecruitment } from '@/api/infopedia/recruitment';
import { RecruitmentVO, RecruitmentQuery, RecruitmentForm } from '@/api/infopedia/recruitment/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { infopedia_company_type, infopedia_recruitment_industry_type, infofepia_recruitment_type } = toRefs<any>(proxy?.useDict('infopedia_company_type', 'infopedia_recruitment_industry_type', 'infofepia_recruitment_type'));

const recruitmentList = ref<RecruitmentVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const recruitmentFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: RecruitmentForm = {
  title: '',
  industry: '',
  position: '',
  employmentType: '',
  companyType: '',
  salaryRange: '',
  requirements: '',
  welfare: '',
  address: '',
  publishTime: '',
  views: 0,
  shares: 0,
  contactPerson: '',
  contactNote: '',
  contactPhone: '',
}
const data = reactive<PageData<RecruitmentForm, RecruitmentQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    industry: undefined,
    position: undefined,
    employmentType: undefined,
    companyType: undefined,
    salaryRange: undefined,
    params: {
    }
  },
  rules: {
    title: [
      { required: true, message: "招聘标题不能为空", trigger: "blur" }
    ],
    industry: [
      { required: true, message: "招聘行业不能为空", trigger: "change" }
    ],
    employmentType: [
      { required: true, message: "雇佣类型不能为空", trigger: "change" }
    ],
    companyType: [
      { required: true, message: "公司类型不能为空", trigger: "change" }
    ],
    salaryRange: [
      { required: true, message: "薪资范围不能为空", trigger: "blur" }
    ],
    requirements: [
      { required: true, message: "任职要求不能为空", trigger: "blur" }
    ],
    welfare: [
      { required: true, message: "福利不能为空", trigger: "blur" }
    ],
    address: [
      { required: true, message: "工作地址不能为空", trigger: "blur" }
    ],
    publishTime: [
      { required: true, message: "首次发布时间不能为空", trigger: "blur" }
    ],
    views: [
      { required: true, message: "浏览量不能为空", trigger: "blur" }
    ],
    shares: [
      { required: true, message: "分享数不能为空", trigger: "blur" }
    ],
    contactPerson: [
      { required: true, message: "联系人不能为空", trigger: "blur" }
    ],
    contactNote: [
      { required: true, message: "联系备注不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询求职招聘信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listRecruitment(queryParams.value);
  recruitmentList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  recruitmentFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: RecruitmentVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加求职招聘信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: RecruitmentVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getRecruitment(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改求职招聘信息";
}

/** 提交按钮 */
const submitForm = () => {
  console.log('submitForm called');
  console.log('recruitmentFormRef:', recruitmentFormRef.value);
  console.log('proxy:', proxy);
  recruitmentFormRef.value?.validate(async (valid: boolean) => {
    console.log('Validation result:', valid)
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateRecruitment(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addRecruitment(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: RecruitmentVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除求职招聘信息编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delRecruitment(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('infopedia/recruitment/export', {
    ...queryParams.value
  }, `recruitment_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
